package com.appclub.kitsilano.settings;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.appclub.kitsilano.MainActivity;
import com.appclub.kitsilano.R;
import com.appclub.kitsilano.bulletin.PDFdownloader;
import com.appclub.kitsilano.other.Service_;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;

public class Settings extends SherlockActivity {
	String PREFS_NAME;
	SharedPreferences settings;
	SharedPreferences.Editor editor;

	@SuppressLint("CommitPrefEdits")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		PREFS_NAME = "Kitsilano";
		settings = getSharedPreferences(PREFS_NAME, 0);
		editor = settings.edit();
		final int IPPDAYFROMHOUR = settings.getInt("IPPDAYFROMHOUR", 9);
		final int IPPDAYFROMMIN = settings.getInt("IPPDAYFROMMIN", 34);
		final int IPPDAYTOHOUR = settings.getInt("IPPDAYTOHOUR", 14);
		final int IPPDAYTOMIN = settings.getInt("IPPDAYTOMIN", 28);

		final int DAY1FROMHOUR = settings.getInt("DAY1FROMHOUR", 8);
		final int DAY1FROMMIN = settings.getInt("DAY1FROMMIN", 54);
		final int DAY1TOHOUR = settings.getInt("DAY1TOHOUR", 15);
		final int DAY1TOMIN = settings.getInt("DAY1TOMIN", 8);

		final int DAY2FROMHOUR = settings.getInt("DAY2FROMHOUR", 8);
		final int DAY2FROMMIN = settings.getInt("DAY2FROMMIN", 54);
		final int DAY2TOHOUR = settings.getInt("DAY2TOHOUR", 15);
		final int DAY2TOMIN = settings.getInt("DAY2TOMIN", 8);

		final int DAY1AFROMHOUR = settings.getInt("DAY1AFROMHOUR", 8);
		final int DAY1AFROMMIN = settings.getInt("DAY1AFROMMIN", 54);
		final int DAY1ATOHOUR = settings.getInt("DAY1ATOHOUR", 15);
		final int DAY1ATOMIN = settings.getInt("DAY1ATOMIN", 8);

		final int DAY2AFROMHOUR = settings.getInt("DAY2AFROMHOUR", 8);
		final int DAY2AFROMMIN = settings.getInt("DAY2AFROMMIN", 54);
		final int DAY2ATOHOUR = settings.getInt("DAY2ATOHOUR", 15);
		final int DAY2ATOMIN = settings.getInt("DAY2ATOMIN", 8);

		RelativeLayout setting_button = (RelativeLayout) findViewById(R.id.setting_button);
		setting_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Settings.this, Settings_Mute.class);
				startActivity(intent);
				overridePendingTransition(R.anim.right_in, R.anim.right_out);
			}
		});
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		boolean state_setting = settings.getBoolean("Setting", false);
		CheckBox setting = (CheckBox) findViewById(R.id.checkBox1);
		setting.setChecked(state_setting);
		if (DAY1FROMHOUR == 8 && DAY1FROMMIN == 54 && DAY2FROMHOUR == 8
				&& DAY2FROMMIN == 54 && DAY1AFROMHOUR == 8
				&& DAY1AFROMMIN == 54 && DAY2AFROMHOUR == 8
				&& DAY2AFROMMIN == 54 && IPPDAYFROMHOUR == 9
				&& IPPDAYFROMMIN == 34 && DAY1TOHOUR == 15 && DAY1TOMIN == 8
				&& DAY2TOHOUR == 15 && DAY2TOMIN == 8 && DAY1ATOHOUR == 15
				&& DAY1ATOMIN == 8 && DAY2ATOHOUR == 15 && DAY2ATOMIN == 8
				&& IPPDAYTOHOUR == 14 && IPPDAYTOMIN == 28) {
		} else {
			java.text.DecimalFormat format = new java.text.DecimalFormat("00");
			setting.setText("Mute device between "
					+ format.format(DAY1FROMHOUR) + ":"
					+ format.format(DAY1FROMMIN) + " to "
					+ format.format(DAY1TOHOUR) + ":"
					+ format.format(DAY1TOMIN) + " on Day1, "
					+ format.format(DAY2FROMHOUR) + ":"
					+ format.format(DAY2FROMMIN) + " to "
					+ format.format(DAY2TOHOUR) + ":"
					+ format.format(DAY2TOMIN) + " on Day2, "
					+ format.format(DAY1AFROMHOUR) + ":"
					+ format.format(DAY1AFROMMIN) + " to "
					+ format.format(DAY1ATOHOUR) + ":"
					+ format.format(DAY1ATOMIN) + " on Day1A, "
					+ format.format(DAY2AFROMHOUR) + ":"
					+ format.format(DAY2AFROMMIN) + " to "
					+ format.format(DAY2ATOHOUR) + ":"
					+ format.format(DAY2ATOMIN) + " on Day2A, "
					+ format.format(IPPDAYFROMHOUR) + ":"
					+ format.format(IPPDAYFROMMIN) + " to "
					+ format.format(IPPDAYTOHOUR) + ":"
					+ format.format(IPPDAYTOMIN) + " on IPP Day.");
		}
		setting.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if (arg1) {
					Intent intent = new Intent(Settings.this, Service_.class);
					startService(intent);
					editor.putBoolean("Setting", true);
					editor.commit();
				} else {
					Intent intent = new Intent(Settings.this, Service_.class);
					stopService(intent);
					editor.putBoolean("Setting", false);
					editor.commit();
				}

			}
		});

		CheckBox autoupdate = (CheckBox) findViewById(R.id.checkBox2);
		boolean state_setting2 = settings.getBoolean("Autoupdate", false);
		autoupdate.setChecked(state_setting2);
		autoupdate
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							Intent i = new Intent(Settings.this,
									PDFdownloader.class);
							i.putExtra("Bulletin", 3);
							startService(i);
						} else {
							Intent i = new Intent(Settings.this,
									PDFdownloader.class);
							stopService(i);
						}
						editor.putBoolean("Autoupdate", isChecked);
						editor.commit();
					}
				});
		CheckBox autoupdate_wifi = (CheckBox) findViewById(R.id.checkBox3);
		boolean state_setting3 = settings.getBoolean("Autoupdatewifi", false);
		autoupdate_wifi.setChecked(state_setting3);
		autoupdate_wifi
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						editor.putBoolean("Autoupdatewifi", isChecked);
						editor.commit();
					}
				});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.right_out);
			this.finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.right_out);
			this.finish();
		}
		return true;
	}
}